package pizza.admin.controller;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pizza.admin.dao.OrderDAO;
import pizza.admin.dao.OrderDAO;
import pizza.admin.dao.UserDAO;
import pizza.admin.entity.Order;
import pizza.admin.entity.OrderPizza;
import pizza.admin.entity.User;

/**
 * Servlet implementation class OrderController
 */
@WebServlet("/order")
public class OrderController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OrderController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		OrderDAO orderDAO = new OrderDAO();
		UserDAO userDAO = new UserDAO();
		String action = request.getParameter("action");
		HttpSession session = request.getSession(true);
		Order order = new Order();
		Collection<Order> allOrders = null;
		
		if (session.getAttribute("user") == null) {
			request.getRequestDispatcher("/templates/index.jsp").forward(
					request, response);
		}
		
		if (action != null) {
			
			if (action.equals("add")) {
				
				Collection<User> users = userDAO.getAll(); 
				request.setAttribute("users",users);
				request.getRequestDispatcher("/templates/order/add.jsp")
						.forward(request, response);
			} else if (action.equals("edit")) {
				
				order = orderDAO.getById(Integer.parseInt(request
						.getParameter("id")));
				
				request.setAttribute("order", order);
				Collection<User> users = userDAO.getAll(); 
				request.setAttribute("users",users);
				
				request.getRequestDispatcher("/templates/order/edit.jsp")
						.forward(request, response);
			} else if (action.equals("delete")) {

				order = orderDAO.getById(Integer.parseInt(request
						.getParameter("id")));
				orderDAO.delete(order);

//				orderDAO.initializeEntityManager();
				allOrders = orderDAO.getAll();
				request.setAttribute("allOrders", allOrders);
				request.getRequestDispatcher("/templates/order/list.jsp")
						.forward(request, response);
			} else if (action.equals("list")||action.isEmpty()) {

				allOrders = orderDAO.getAll();
				
				request.setAttribute("allOrders", allOrders);
				request.getRequestDispatcher("/templates/order/list.jsp")
						.forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		OrderDAO orderDAO = new OrderDAO();
		UserDAO userDAO = new UserDAO();
		
		String action = request.getParameter("action");

		HttpSession session = request.getSession(true);
		Order order = new Order();

		if (action != null) {
			if (session.getAttribute("user") == null) {
				request.getRequestDispatcher("/templates/index.jsp").forward(
						request, response);
			}
			if (action.equals("add")) {
				Collection<User> users = userDAO.getAll(); 
				request.setAttribute("users",users);
//				userDAO.initializeEntityManager();
				User chosedUser = userDAO.getById(Integer.parseInt(request.getParameter("user")));
				
				order.setUser(chosedUser);
				order.setAddress(request.getParameter("address"));
				order.setDescription(request.getParameter("description"));
				order.setPhone(request.getParameter("phone"));

				if (!order.validateForDB()) {
					request.setAttribute("order", order);
					request.getRequestDispatcher("/templates/order/add.jsp")
							.forward(request, response);
				} else {
					
					orderDAO.add(order);
					
					Collection<Order> allOrders = null;
//					orderDAO.initializeEntityManager();
					allOrders = orderDAO.getAll();
					request.setAttribute("allOrders", allOrders);
					request.getRequestDispatcher("/templates/order/list.jsp")
							.forward(request, response);
				}
			} else if (action.equals("edit")) {
				order = orderDAO.getById(Integer.parseInt(request
						.getParameter("id")));
				
//				orderDAO.initializeEntityManager();
				Collection<User> users = userDAO.getAll(); 
				request.setAttribute("users",users);
//				userDAO.initializeEntityManager();
				User chosedUser = userDAO.getById(Integer.parseInt(request.getParameter("user")));
				
				order.setUser(chosedUser);
				order.setAddress(request.getParameter("address"));
				order.setDescription(request.getParameter("description"));
				order.setPhone(request.getParameter("phone"));

				if (!order.validateForDB()) {
					request.setAttribute("order", order);
					request.getRequestDispatcher("/templates/order/add.jsp")
							.forward(request, response);
				} else {
					
					orderDAO.edit(order);
					
					Collection<Order> allOrders = null;
//					orderDAO.initializeEntityManager();
					allOrders = orderDAO.getAll();
					request.setAttribute("allOrders", allOrders);
					request.getRequestDispatcher("/templates/order/list.jsp")
							.forward(request, response);
				}
			}
		}
	}

}
